package com.kittlen.managementSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kittlen.managementSystem.entity.Coach;
import com.kittlen.managementSystem.entity.MySqlLimit;
import com.kittlen.managementSystem.entity.Vehicle;
import com.kittlen.managementSystem.entity.Result;
import com.kittlen.managementSystem.service.IVehicleService;
import com.kittlen.managementSystem.utils.ObjectUtil;
import com.kittlen.managementSystem.utils.TimeUtil;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆表 前端控制器
 * </p>
 *
 * @author kittlen
 * @since 2021-01-26
 */
@RestController
@RequestMapping("/vehicle")
public class VehicleController {
    @Resource
    IVehicleService iVehicleService;

    /**
     * 车辆查询
     *
     * @param vehicle
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/selects")
    public Result selects(Vehicle vehicle, @RequestParam(required = false, defaultValue = "0") int page,
                          @RequestParam(required = false, defaultValue = "0") int limit,
                          @RequestParam(required = false, defaultValue = "") String startTime,
                          @RequestParam(required = false, defaultValue = "") String endTime) {
        Result result = new Result();
        QueryWrapper<Vehicle> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNoEmpty(vehicle.getVehicleId())) {
            queryWrapper.like("vehicle_id", vehicle.getVehicleId());
        }
        if (ObjectUtil.isNoEmpty(vehicle.getVehicleName())) {
            queryWrapper.like("vehicle_name", vehicle.getVehicleName());
        }
        if (ObjectUtil.isNoEmpty(vehicle.getLicensePlateNumber())) {
            queryWrapper.like("license_plate_number", vehicle.getLicensePlateNumber());
        }
        if (ObjectUtil.isNoEmpty(vehicle.getType())) {
            queryWrapper.eq("type", vehicle.getType());
        }
        if (ObjectUtil.isNoEmpty(vehicle.getCoach())) {
            if (ObjectUtil.isNoEmpty(vehicle.getCoach().getCoachId())) {
                queryWrapper.eq("c.coach_id", vehicle.getCoach().getCoachId());
            }
        }
        if (page!=-1) {
            MySqlLimit mySqlLimit=new MySqlLimit(page,limit);
            queryWrapper.last("LIMIT "+mySqlLimit.getOffset()+","+mySqlLimit.getRows());
        }
        List<Vehicle> list = iVehicleService.selects(queryWrapper);
        result.setData(list);
        queryWrapper.last("");
        result.setCount(iVehicleService.count(queryWrapper));
        return result;
    }

    /**
     * 车辆添加
     *
     * @param vehicle
     * @return
     */
    @RequestMapping("/insert")
    public Result insert(Vehicle vehicle) {
        Result result = new Result();
        try {
            if (iVehicleService.save(vehicle)) {
                result.setData(vehicle);
                result.setMsg("车辆添加成功");
            } else {
                result.setCode(result.RESULT_ERROR);
                result.setMsg("车辆添加失败");
            }
        }catch (DuplicateKeyException e){
            result.setCode(result.RESULT_ERROR);
            result.setMsg("车辆添加失败,车辆ID重复");
        }
        return result;
    }

    /**
     * 车辆添加
     * @param vehicleStr
     * @return
     */
    @RequestMapping("/vehicleImport")
    public Result vehicleImport(String vehicleStr){
        Result result=new Result();
        List<Vehicle> list=new Gson().fromJson(vehicleStr,new TypeToken<List<Vehicle>>(){}.getType());
        try {
            if(iVehicleService.batchInsert(list)>0) {
                result.setMsg("车辆添加成功");
            }else{
                result.setCode(result.RESULT_ERROR);
                result.setMsg("车辆添加失败");
            }
        }catch (DuplicateKeyException e){
            String err=e.getCause().toString();
            String errID=err.substring(err.indexOf("Duplicate entry")+15,err.indexOf("for key"));
            result.setCode(result.RESULT_ERROR);
            result.setMsg("车辆添加失败,车辆ID"+errID+"重复了");
        }

        return result;
    }

    /**
     * 车辆修改
     *
     * @param vehicle
     * @return
     */
    @RequestMapping("/update")
    public Result update(Vehicle vehicle,String coachId) {
        Result result = new Result();
        if(ObjectUtil.isNoEmpty(coachId)){
            if(iVehicleService.update(vehicle,new UpdateWrapper<Vehicle>().set("coach_id",coachId).eq("vehicle_id",vehicle.getVehicleId()))){
                result.setData(vehicle);
                result.setMsg("车辆信息修改成功");
            } else {
                result.setCode(result.RESULT_ERROR);
                result.setMsg("车辆信息修改失败");
            }
        }else {
            if (iVehicleService.updateById(vehicle)) {
                result.setData(vehicle);
                result.setMsg("车辆信息修改成功");
            } else {
                result.setCode(result.RESULT_ERROR);
                result.setMsg("车辆信息修改失败");
            }
        }
        return result;
    }

    /**
     * 车辆删除
     *
     * @param vehicle
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Vehicle vehicle) {
        Result result = new Result();
        if (iVehicleService.removeById(vehicle)) {
            result.setMsg("删除成功");
        } else {
            result.setMsg("删除失败");
            result.setCode(result.RESULT_ERROR);
        }
        return result;
    }

}

